/*
    /login => 登录页面
    /register = > 注册页面
*/
const fs = require('fs')
let result = fs.readFileSync('persons.json', 'utf8')
result = JSON.parse(result)
const http = require('http')
const server = http.createServer()
server.listen(3000)
server.on('request', (req, res) => {

    res.writeHead(200, { 'Content-type': 'text/html;charset=utf8' })
    let json = {};
    let arr = req.url.substring(req.url.indexOf("?") + 1).split("&");
    for (let i = 0; i < arr.length; i++) {
        var a = arr[i].split("=");
        json[a[0]] = a[1];
    }
    var a = result.some(item => item.username == json.username && item.password == json.password
    )
    if (req.url.startsWith('/login')) {
        if (!a) {
            res.end('用户名或密码错误')
        }
        else {
            res.end('登录成功')
        }
    }
    else if (req.url.startsWith('/register')) {
        if (!a) {
            result.push(json)
            fs.writeFileSync('persons.json', JSON.stringify(result))
            res.end('注册成功')
        }
        else {
            res.end('登录成功')
        }
    }


})